package com.wn.pettyloan.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wn.pettyloan.util.Result;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: pettyloan
 * @ClassName MyAccessDeniedHandler
 * @description:
 * @author: 韩旭
 * @create: 2022-12-16 11:25
 **/
@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        response.setContentType("application/json;charset=UTF-8");
        String string = new ObjectMapper().writeValueAsString(Result.error("没有相关权限"));
        response.getWriter().println(string);
    }
}